﻿// Copyright (C) 2010-2012, MaGUI
//
//  Elena Ballesteros Molinero
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Magui
{
    public class Controlador
    {
        #region variables
        private Point _pMouse, _pCaret, _pFocus;

        public Point Point { get; set; }
        public Point PointMouse { get { return _pMouse; } }
        #endregion

        #region Patrón Singletone
        /// <summary>
        /// Control Singletone sobre el controlador
        /// </summary>
        private static Controlador _instance;
        public static Controlador Instance
        {
            get { return _instance ?? (_instance = new Controlador()); }
        }
        #endregion

        #region Variables
        private Soporte.Soporte soporte;
        private bool fontSmoothing;              
        private Magnificador magui;
        #endregion

        public Controlador()
        {            
            soporte = Soporte.Soporte.Instance;
            magui = Magnificador.Instance;            

            fontSmoothing = soporte.GetFontSmoothing();

            soporte.GetControlTeclado().KeyDown += MyKeyDown;
            soporte.GetControlTeclado().KeyUp += MyKeyUp;

            //soporte.GetControlFoco().RefreshMagnifier += Controlador_RefreshMagnifier;            
        }

        public void SetLayeredForm(IntPtr handle)
        {
            soporte.SetLayeredTransparent(handle,
                                          magui.Contenedor.Posicion,
                                          magui.Contenedor.Dimension);
        }

        public void SetSmoothing(bool enable)
        {
            magui.Alisado = enable;
        }

        public void SetInverseColor()
        {
            magui.InverseColor = !magui.InverseColor;
        }

        public void ShowMagnifier(bool enable)
        {
            magui.Activo = enable;
            if (magui.Activo)
            {
                bool desactiva = Soporte.Soporte.EnableAero(false);
                //Desactivamos el Cleartype            
                if (soporte.GetFontSmoothing())
                    soporte.DisableFontSmoothing();

                FormLupa.Instance.Show();

                try
                {
                    soporte.GetControlTeclado().EnableHook();
                    //soporte.GetControlFoco().EnableHook();
                }
                catch (Exception ex)
                {
                    //MessageBox.Show("Error Enable Hook" + ex.ToString());
                }

            }
            else
            {

                if (fontSmoothing)
                    soporte.EnableFontSmoothing();

                FormLupa.Instance.Hide();

                try
                {
                    soporte.GetControlTeclado().DisableHook();
                    //soporte.GetControlFoco().DisableHook();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error Disable Hook" + ex.ToString());
                }
                finally
                {
                    soporte.GetMouse().RecuperaCursoresDelSistema();
                }
            }
        }

        public Point UpdatePosition()
        {
            Point pM = Cursor.Position;
            Point pC = soporte.GetKeyboard().GetCaretPos();

            if (pC != _pCaret)
            {
                _pCaret = pC;
                return _pCaret;
            }
            else if (pM != _pMouse)
            {
                _pMouse = pM;
                return _pMouse;
            }

            return Point;
        }


        #region Eventos teclado
        private void MyKeyDown(object sender, KeyEventArgs e)
        {
            if (Magnificador.Instance.Pointer.RestringirMovimiento)
            {
                if (soporte.GetKeyboard().IsShiftPressed())
                    Cursor.Clip = soporte.GetKeyboard().GetActiveFormBounds();
            }
        }

        private void MyKeyUp(object sender, KeyEventArgs e)
        {
            if (Magnificador.Instance.Pointer.RestringirMovimiento)
            {
                if (soporte.GetKeyboard().IsShiftPressed())
                {
                    // Reestablecer área movimiento cursor al formulario activo
                    Cursor.Clip = Screen.PrimaryScreen.Bounds;
                }
            }
        }
        #endregion

        #region Eventos foco
        void Controlador_RefreshMagnifier(object sender, Point point)
        {
            if (Magnificador.Instance.Focus.Seguimiento)
            {
                Point = UpdatePosition(point.X, point.Y);
                if (Point != Point.Empty)
                {
                    magui.Repintar(Point.X, Point.Y, true);
                }
                FormLupa.Instance.Refresh();
            }
        }

        private Point UpdatePosition(double x, double y)
        {
            _pFocus = new Point((int)x, (int)y);
            //return Point.Empty;
            return _pFocus;
        }

        

        
        #endregion
    }
}
